package com.cincc.taofangwang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cincc.taofangwang.R;
import com.cincc.taofangwang.common.callback.OnClickCallBack;
import com.cincc.taofangwang.model.HistoryModel;

import java.util.List;

public class HistoryAdapter extends BaseAdapter {
    private List<HistoryModel> list;
    private Context mContext;

    public HistoryAdapter(Context mContext, List<HistoryModel> list) {
        this.list = list;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HistoryModel fruit = list.get(position);//获取当前项的 Fruit 实例
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_history_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.numberText = view.findViewById(R.id.number_tv);
            viewHolder.middleText = view.findViewById(R.id.middle_tv);
            viewHolder.smallText = view.findViewById(R.id.small_tv);
            viewHolder.beginText = view.findViewById(R.id.begin_time_tv);
            viewHolder.relayout = view.findViewById(R.id.call_btn);
            view.setTag(viewHolder);//将 viewHolder 存储在 view 中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();//重新获取 viewHolder
        }
        // 设置要显示的图片和文字
        viewHolder.numberText.setText(fruit.callNo);
        viewHolder.middleText.setText(fruit.telX);
        viewHolder.smallText.setText(fruit.userData);
        viewHolder.beginText.setText(fruit.startTime);
        viewHolder.relayout.setOnClickListener(v -> onClickBallBack.callback(position));
        return view;
    }

    private class ViewHolder {
        TextView numberText;
        TextView middleText;
        TextView smallText;
        TextView beginText;
        RelativeLayout relayout;
    }

    private OnClickCallBack onClickBallBack;

    public void setCallBack(OnClickCallBack callBack) {
        this.onClickBallBack = callBack;
    }
}
